import {
    GetSellProductList,
    SetProductsOffline,
    SetProductsOnline,
    DeletesProduct,
    GetTagList,
    GetProductGroupList,
    GetSearchSoldOutProductListWithPage,
    GetSearchWarehouseProductListWithPage,
    UpdateProductsTags,
    UpdateProductSort,
    UpdateProductStock,
    UpdateSProductPrice,
    GetProductSkuInfoByProductId,
    UpdateProductsGroup,
    IsMultiProduct,
    CopyProduct,
    GetMemberPriceByProductId,
    UpdateMProductPrice
} from '@/services/product/productSell';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'pruductSellList',

    state: {
        pruductSellList: [],
        productGroupList: [],
        productTagList: [],
        soldOutProductList: [],
        warehouseProductList: [],
        productSkuInfo: [],
        sourceData:[],
        isMulti: {},
        MemberPriceByProduct: {}
    },

    effects: {
        //获取在售
        *GetSellProductList({payload, callBack}, {call, put}) {
            const data = yield call(GetSellProductList, payload);
            yield catchException(function* (){
              if(data.Code ===0) {
                yield put({
                    type: 'saveGetSellProductList',
                    payload: {
                        DataList: data.Data.DataList,
                        Total: data.Data.Total
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },

        //获取已售完
        *GetSearchSoldOutProductListWithPage({payload, callBack}, {call, put}) {
            const data = yield call(GetSearchSoldOutProductListWithPage, payload);
            yield catchException(function* (){
              if(data.Code ===0) {
                yield put({
                    type: 'saveGetSearchSoldOutProductListWithPage',
                    payload: {
                        DataList: data.Data.DataList,
                        Total: data.Data.Total
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },


        //获取仓库中
         *GetSearchWarehouseProductListWithPage({payload, callBack}, {call, put}) {
            const data = yield call(GetSearchWarehouseProductListWithPage, payload);
            yield catchException(function* (){
              if(data.Code ===0) {
                yield put({
                    type: 'saveGetSearchWarehouseProductListWithPage',
                    payload: {
                        DataList: data.Data.DataList,
                        Total: data.Data.Total
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },



        //获取全部分组和未分组
        *GetProductGroupList({payload, callBack}, {call, put}) {
          const data = yield call(GetProductGroupList, payload);
          yield catchException(function* (){
            if(data.Code ===0) {
              yield put({
                  type: 'saveGetProductGroupList',
                  payload: {
                      DataList: data.Data
                  },
              });
              callBack && callBack.success(data);
          } else {
              callBack && callBack.error(data)
          }
          })

      },

        // 获取标签
        *GetTagList({payload, callBack}, {call, put}) {
          const data = yield call(GetTagList, payload);
          yield catchException(function* (){
            if(data.Code ===0) {
              yield put({
                  type: 'saveGetTagList',
                  payload: {
                      DataList: data.Data
                  },
              });
              callBack && callBack.success(data);
          } else {
              callBack && callBack.error(data)
          }
          })

      },

      // 获取单个商品规格
      *GetProductSkuInfoByProductId({payload, callBack}, {call, put}) {
        const data = yield call(GetProductSkuInfoByProductId, payload);
        yield catchException(function* (){
          if(data.Code ===0) {
            yield put({
                type: 'saveGetProductSkuInfoByProductId',
                payload: {
                    DataList: data.Data
                },
            });
            yield put({
                type: 'changeData',
                payload: data.Data,
            });
            callBack && callBack.success(data);
        } else {
            callBack && callBack.error(data)
        }
        })

    },

        //是否多规格
        *IsMultiProduct({payload, callBack}, {call, put}) {
            const data = yield call(IsMultiProduct, payload);
            yield catchException(function* (){
              if(data.Code ===0) {
                yield put({
                    type: 'saveIsMultiProduct',
                    payload: {
                        data: data.Data
                    },
                });
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
            })

        },

      // 修改标签
      *UpdateProductsTags({payload, callBack}, {call}) {
        const data = yield call(UpdateProductsTags, payload);
        yield catchException(function* (){
          if(data.Code === 0) {
            callBack && callBack.success(data);
        } else {
            callBack && callBack.error(data)
            }
        })

      },

      // 修改分类
      *UpdateProductsGroup({payload, callBack}, {call}) {
        const data = yield call(UpdateProductsGroup, payload);
        yield catchException(function* (){
          if(data.Code === 0) {
            callBack && callBack.success(data);
        } else {
            callBack && callBack.error(data)
            }
        })

      },

      // 修改排序
      *UpdateProductSort({payload, callBack}, {call}) {
        const data = yield call(UpdateProductSort, payload);
        yield catchException(function* (){
          if(data.Code === 0) {
            callBack && callBack.success(data);
        } else {
            callBack && callBack.error(data)
            }
        })

      },

      // 修改库存
      *UpdateProductStock({payload, callBack}, {call}) {
        const data = yield call(UpdateProductStock, payload);
        yield catchException(function* (){
          if(data.Code === 0) {
            callBack && callBack.success(data);
        } else {
            callBack && callBack.error(data)
            }
        })

      },

      // 修改价格
      *UpdateSProductPrice({payload, callBack}, {call}) {
        const data = yield call(UpdateSProductPrice, payload);
        yield catchException(function* (){
          if(data.Code === 0) {
            callBack && callBack.success(data);
        } else {
            callBack && callBack.error(data)
            }
        })

      },



        //下架
        *SetProductsOffline({payload, callBack},{call}) {
            const data = yield call(SetProductsOffline, payload);
            yield catchException(function* (){
              if (data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                callBack && callBack.error(data)
              }
            })

        },

            //上架
            *SetProductsOnline({payload, callBack},{call}) {
                const data = yield call(SetProductsOnline, payload);
                yield catchException(function* (){
                  if (data.Code === 0) {
                    callBack && callBack.success(data);
                  } else {
                    callBack && callBack.error(data)
                  }
                })

            },

        //删除
        *DeletesProduct({payload, callBack},{call}) {
            const data = yield call(DeletesProduct, payload);
            yield catchException(function* (){
              if (data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                callBack && callBack.error(data)
              }
            })

        },

        // 复制
        *CopyProduct({payload, callBack},{call}) {
            const data = yield call(CopyProduct, payload);
            yield catchException(function* (){
              if (data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                callBack && callBack.error(data)
              }
            })

        },

        // 设置价
        *UpdateMProductPrice({payload, callBack},{call}) {
            const data = yield call(UpdateMProductPrice, payload);
            yield catchException(function* (){
              if (data.Code === 0) {
                callBack && callBack.success(data);
                } else {
                callBack && callBack.error(data)
              }
            })

        },

        //
        *GetMemberPriceByProductId({payload, callBack},{call, put}) {
            const data = yield call(GetMemberPriceByProductId, payload);
            yield catchException(function* (){
              if(data.Code ===0) {
                yield put({
                    type: 'saveGetMemberPriceByProductId',
                    payload: {
                        data: data.Data
                    },
                });
                yield put({
                    type: 'changeSangelData',
                    payload: data.Data,
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },
    },

    reducers: {
        saveGetSellProductList(state, {payload}) {
            return {
                ...state,
                pruductSellList: payload.DataList,
            };
        },
        saveGetProductGroupList(state, {payload}) {
          return {
              ...state,
              productGroupList: payload.DataList,
          };
      },
      saveGetTagList(state, {payload}) {
        return {
            ...state,
            productTagList: payload.DataList,
        };
    },
    saveGetSearchSoldOutProductListWithPage(state, {payload}) {
        return {
            ...state,
            soldOutProductList: payload.DataList,
        }
    },
    saveGetSearchWarehouseProductListWithPage(state, {payload}) {
        return {
            ...state,
            warehouseProductList: payload.DataList
        }
    },
    saveGetProductSkuInfoByProductId(state, {payload}) {
        return {
            ...state,
            productSkuInfo: payload.DataList
        }
    },

    changeData(state, {payload,callBack}) {
        callBack && callBack.success();
        return {
            ...state,
            sourceData: payload
        }
    },

    changeSangelData(state, {payload,callBack}) {
        callBack && callBack.success();
        return {
            ...state,
            sourceDataa: payload
        }
    },

    saveIsMultiProduct(state, {payload, callBack}) {
        callBack && callBack.success();
        return {
            ...state,
            isMulti: payload
        }
    },

    saveGetMemberPriceByProductId(state, {payload, callBack}) {
        callBack && callBack.success();
        return {
            ...state,
            MemberPriceByProduct: payload
        }
    },


    }
};
